package com.byyy.ccts.modules.ysjl.api.controller;

import com.alibaba.fastjson.JSON;
import com.byyy.ccts.core.base.ILogBase;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.cc.entity.ccts.TsRelation;
import com.byyy.ccts.modules.ysjl.entity.YsjlBanner;
import com.byyy.ccts.modules.ysjl.entity.YsjlJielong;
import com.byyy.ccts.modules.ysjl.entity.YsjlJielongDetail;
import com.byyy.ccts.modules.ysjl.service.IYsjlJielongService;
import com.byyy.ccts.modules.ysjl.vo.JielongVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author lyb
 * @date 2022-06-22 13:33
 * @description XX
 */
@Controller
@RequestMapping("/api/ysjl")
public class YsjlController  implements ILogBase {

    @Autowired
    private IYsjlJielongService ysjlJielongService;

    /**
     * 查询滚动展示图
     */
    @RequestMapping(value = "/queryBanner", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson queryBanner() {
        AjaxJson json = new AjaxJson();
        List<YsjlBanner> jielong = this.ysjlJielongService.queryBanner();
        json.setData(jielong);
        return json;
    }

    /**
     * 查询接龙
     */
    @RequestMapping(value = "/queryJielong", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson queryJielong(String person) {
        AjaxJson json = new AjaxJson();
        List<YsjlJielong> jielong = this.ysjlJielongService.queryJielong(person);
        json.setData(jielong);
        return json;
    }

    /**
     * 查询我的接龙
     */
    @RequestMapping(value = "/queryMyJielong", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson queryMyJielong(String person) {
        AjaxJson json = new AjaxJson();
        List<YsjlJielong> jielong = this.ysjlJielongService.queryMyJielong(person);
        json.setData(jielong);
        return json;
    }

    /**
     * 根据ID查询接龙
     */
    @RequestMapping(value = "/queryJielongById", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson queryJielongById(String id) {
        AjaxJson json = new AjaxJson();
        YsjlJielong jielong = this.ysjlJielongService.queryJielongById(id);
        List<YsjlJielongDetail> detailList = this.ysjlJielongService.queryJielongDetailByPid(id);
        for (YsjlJielongDetail detail : detailList) {
            try {
                detail.setPerson(URLDecoder.decode(detail.getPersonEncode(), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error("微信昵称转编码失败！", e);
            }
        }
        JielongVo vo = new JielongVo();
        vo.setYsjlJielong(jielong);
        vo.setYsjlJielongDetailList(detailList);

        json.setData(vo);
        return json;
    }

    /**
     * 接龙报名
     */
    @RequestMapping(value = "/jielong", method = { RequestMethod.POST })
    @ResponseBody
    public AjaxJson jielong(String id, String jielongList) {
        AjaxJson json = new AjaxJson();
        List<YsjlJielongDetail> detailList = JSON.parseArray(jielongList, YsjlJielongDetail.class);
        for (YsjlJielongDetail detail : detailList) {
            try {
                detail.setPersonEncode(URLEncoder.encode(detail.getPerson(), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error("微信昵称转编码失败！", e);
            }
        }
        String ret = this.ysjlJielongService.jielong(id, detailList);
        return json;
    }

    /**
     * 发布接龙
     */
    @RequestMapping(value = "/fabuJielong", method = { RequestMethod.POST })
    @ResponseBody
    public AjaxJson fabuJielong(String yanzhengma, String jielong) {
        AjaxJson json = new AjaxJson();
        if (!"ysjl".equals(yanzhengma)) {
            json.fail("验证码错误！请联系管理员！");
            return json;
        }
        YsjlJielong ysjlJielong =JSON.parseObject(jielong, YsjlJielong.class);
        if (ysjlJielong.getRiqi() == null) {
            json.fail("请输入打球日期！");
            return json;
        }
        if (StringUtils.isEmpty(ysjlJielong.getBeginDate())) {
            json.fail("请输入开始时间！");
            return json;
        }
        if (StringUtils.isEmpty(ysjlJielong.getEndDate())) {
            json.fail("请输入结束时间！");
            return json;
        }
        if (StringUtils.isEmpty(ysjlJielong.getLocation())) {
            json.fail("请输入场地！");
            return json;
        }
        if (ysjlJielong.getNum() < 1) {
            json.fail("请输入数量！");
            return json;
        }
        if (ysjlJielong.getLimit() < 1) {
            json.fail("请输入人数限制！");
            return json;
        }

        this.ysjlJielongService.fabuJielong(ysjlJielong);
        return json;
    }

}
